import * as fileSys from "fs";

import * as path from "path";
import {miniSiteList} from "./data-var-list/mini-site-list";
const pacDirname: string = "/Users/chiyuxinyi/Documents/procedures/new-pac";
const pacFilename: string = path.join(pacDirname, "pac");
const pac5Filename: string = path.join(pacDirname, "pac5");
declare const domains: {
    [domain: string]: string | number;
};

let data: string;
let domainSet: Set<string> = new Set<string>();
miniSiteList.forEach(item => {
    domainSet.add(item);
});

function domainSetAdd(text: string) {
    global.eval(text);
    let valSet = new Set<string | number>();
    for (let attr in domains) {
        if (Object.prototype.hasOwnProperty.call(domains, attr)) {
            domainSet.add(attr);
        }
    }
}
Promise.all([
    fileSys.promises.readFile(pacFilename, "utf8"),
    fileSys.promises.readFile(pac5Filename, "utf8"),
]).then((textList: [ string, string ]) => {
    textList.forEach(text => {
       domainSetAdd(text);
    });
    fileSys.promises.writeFile(path.join(__dirname, `../surge-rule/rule@${Date.now()}.txt`), Array.from(domainSet).map((item) => {
        return `DOMAIN-SUFFIX,${item},ssr`;
    }).join("\r\n"), "utf8");
});

